package cn.wolfcode.edu.web.controller;


import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by abb on 2018-07-07.
 */
@ControllerAdvice
public class CommonExceptionHandler {

    @ExceptionHandler(UnauthorizedException.class)
    @ResponseBody
    Map<String,Object> exceptionHandler(UnauthorizedException e, HttpServletRequest request, HttpServletResponse response,
                                        HandlerMethod handlerMethod, Model model)throws IOException{
        ResponseBody body = handlerMethod.getMethodAnnotation(ResponseBody.class);
        if(body == null){
            response.sendRedirect("/unpermission.jsp");
            return  null;
        }else{
            Map<String,Object> map = new HashMap<>();
            map.put("success",false);
            map.put("msg","no permission");
            map.put("total",0);
            map.put("rows",new ArrayList<>());
            return map;
        }

    }
}
